"""
__author__ = '霍格沃兹测试开发学社'
__desc__ = '更多测试开发技术探讨，请访问：https://ceshiren.com/t/topic/15860'
"""
from typing import List

# from orm_domain import Suite
from orm_domain.execution import Execution
from orm_domain.job_suite_rel import job_suite_rel

from dataclasses import dataclass, field
from datetime import datetime

from sqlalchemy import *
from sqlalchemy.orm import relationship

from orm_domain.suite import Suite
from server import mapper_registry


# # todo ： 遗留问题⭐️ orm 实体类是否要继承于mdomain 中的抽象类
# class Job(db.Model):
#     # 表名
#     __tablename__ = "job"
#     # job id
#     id = db.Column(Integer, primary_key=True)
#     # job 名称
#     name = db.Column(String(120))
#     project_id = db.Column(db.Integer,ForeignKey('project.id'))
#     execution_count = db.Column(db.Integer,default=0)
#     create_time = db.Column(db.DateTime, nullable=True, default=datetime.now)
#     update_time = db.Column(db.DateTime, nullable=True, default=datetime.now, onupdate=datetime.now)
#     suites = relationship('Suite', secondary=job_suite_rel)


@mapper_registry.mapped
@dataclass
class Job:
    __tablename__ = "job"
    __sa_dataclass_metadata_key__ = "sa"

    id: int = field(init=False, metadata={"sa": Column(Integer, primary_key=True)}, )
    name: str = field(default=None, metadata={"sa": Column(String(120))}, )

    # 外键 指向job 表
    project_id: int = field(default=None, metadata={"sa": Column(Integer, ForeignKey("project.id"))}, )
    execution_count: int = field(init=False, metadata={"sa": Column(Integer, default=0)}, )
    create_time: datetime = field(init=False, metadata={"sa": Column(DateTime, nullable=True, default=datetime.now)}, )
    update_time: datetime = field(init=False, metadata={"sa": Column(DateTime, nullable=True, default=datetime.now, onupdate=datetime.now)}, )
    suites: List[Suite] = field(default_factory=list, metadata={"sa": relationship('Suite', secondary=job_suite_rel)}, )
    executions:Execution = field(default_factory=list, metadata={"sa": relationship('Execution', backref="execution", cascade='all, delete-orphan')}, )
